package servlet;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import dao.AddressDao;
import dao.CartDao;
import dao.GoodsDao;
import entity.*;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/settleCartItems")
public class MyCartsSettleServlet extends HttpServlet {
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		User user = (User) request.getSession().getAttribute("user");

		// 读取请求体中的JSON数据
		StringBuilder jsonBuffer = new StringBuilder();
		String line;
		try (BufferedReader reader = request.getReader()) {
			while ((line = reader.readLine()) != null) {
				jsonBuffer.append(line);
			}
		}

		// 使用Gson解析JSON数据
		Gson gson = new Gson();
		Type listType = new TypeToken<List<Map<String, String>>>() {}.getType();
		List<Map<String, String>> items = gson.fromJson(jsonBuffer.toString(), listType);
		List<Cart> cartItems = new ArrayList<>();
		BigDecimal allGoodsPrice = BigDecimal.valueOf(0.0);
		Integer allGoodsNumb = 0;
		// 处理结算逻辑
		if (items != null && !items.isEmpty()) {
			// 处理每个商品的结算逻辑
			for (Map<String, String> item : items) {
				String cartId = item.get("cartId");
				CartDao cartDao = new CartDao();
				Cart cartItem = cartDao.selectByCartId(cartId);
				allGoodsPrice = allGoodsPrice.add(cartItem.getCartSum());
				allGoodsNumb = allGoodsNumb + cartItem.getQty();
				cartItems.add(cartItem);
			}
			AddressDao addressDao = new AddressDao();
			List<Address> list = addressDao.selectMyAddress(user.getUserId());
			request.getSession().setAttribute("cartItems", cartItems);
			request.getSession().setAttribute("allGoodsPrice", allGoodsPrice);
			request.getSession().setAttribute("allGoodsNumb", allGoodsNumb);
			request.getSession().setAttribute("myAddresses", list);
			System.out.println("成功");
			// 返回成功响应给前端
			JsonObject jsonResponse = new JsonObject();
			jsonResponse.addProperty("message", "结算成功");
			response.getWriter().write(gson.toJson(jsonResponse));
		} else {
			// 返回错误响应
			JsonObject jsonResponse = new JsonObject();
			jsonResponse.addProperty("message", "没有选中的商品");
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().write(gson.toJson(jsonResponse));
		}
	}

}
